import boto3
import json

# 设置 AWS 访问密钥
# [profile Qa]
# aws_access_key_id = "AKIASWZL2UIXMPQQ4NFT"
# aws_secret_access_key = "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"
#
# [profile Prod]
aws_access_key_id = "AKIAVGNWUKMQCF2NKKNR"
aws_secret_access_key = "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"

aws_region = 'us-east-2'

# 创建 AWS Lambda 客户端
lambda_client = boto3.client(
    'lambda',
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name=aws_region
)

# 初始化分页参数
next_marker = None

# 创建空列表以存储 Lambda 信息
lambda_info = []
# 遍历每个 Lambda 函数
while True:
    # 获取 Lambda 函数列表
    if next_marker:
        response = lambda_client.list_functions(Marker=next_marker)
    else:
        response = lambda_client.list_functions()

    # 提取当前页的函数信息
    functions = response['Functions']

    for function in functions:
        # 获取 Lambda 函数的详细信息
        function_info = lambda_client.get_function(FunctionName=function['FunctionName'])
        # 整合详细信息和配置信息为一个字典
        combined_info = {
            'FunctionInfo': function_info
        }

        # 将整合的信息添加到列表中
        lambda_info.append(combined_info)

    # 检查是否有下一页
    if 'NextMarker' in response:
        next_marker = response['NextMarker']
    else:
        break

# 将 Lambda 信息输出为 JSON 格式
with open('lambda_info.json', 'w') as outfile:
    json.dump(lambda_info, outfile, indent=4)

if __name__ == "__main__":
    var = None
